package com.example.administrator.coldlain.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.coldlain.R;
import com.example.administrator.coldlain.model.InsertUser;
import com.example.administrator.coldlain.model.UserLogin;
import com.google.gson.Gson;

import org.json.JSONObject;

public class LoginActivity extends Activity implements View.OnClickListener {
    private Button logingBtn, registBtn;
    SharedPreferences sharedPreferences;
    private EditText login_user_name;
    private EditText login_user_password;
    private CheckBox login_r;
    private Button login_btn;
    private Button regist_btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        getLoginInfo();



    }

    private void initView() {
        login_user_name = (EditText) findViewById(R.id.login_user_name);
        login_user_password = (EditText) findViewById(R.id.login_user_password);
        login_r = (CheckBox) findViewById(R.id.login_r);
        login_btn = (Button) findViewById(R.id.login_btn);
        regist_btn = (Button) findViewById(R.id.regist_btn);

        login_btn.setOnClickListener(this);
        regist_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn:

                if (login_r.isChecked())
                    saveLoginInfo();
//                        "http://10.70.64.78:8080/MyTest/LoginServlet?account='"+login_user_name.getText().toString()+"'&password='"+login_user_password.getText().toString()+"'",
                String username=login_user_name.getText().toString();
                String psw=login_user_password.getText().toString();
                JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(
                        Request.Method.POST,
                        "http://192.168.137.1:8080/MyTest/LoginServlet?account="+username+"&password="+psw+"",
                        "",
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject jsonObject) {
                                Gson gson=new Gson();
                                UserLogin userLogin=gson.fromJson(jsonObject.toString(),UserLogin.class);
                                String msg=userLogin.getMsg();
                                if(msg.equals("OK")) {
                                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                    Toast.makeText(LoginActivity.this, "恭喜你登录成功！", Toast.LENGTH_SHORT).show();
                                }
                                else if(msg.equals("failed"))
                                    Toast.makeText(LoginActivity.this, "登录失败！", Toast.LENGTH_SHORT).show();
                                else
                                    Toast.makeText(LoginActivity.this, "异常！", Toast.LENGTH_SHORT).show();

                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Toast.makeText(getApplication(),volleyError.toString(),Toast.LENGTH_SHORT).show();
                            }
                        }
                );
                Volley.newRequestQueue(getApplication()).add(jsonObjectRequest);


                break;
            case R.id.regist_btn:
                startActivity(new Intent(LoginActivity.this, RegistActivity.class));
                break;
        }
    }

    private void saveLoginInfo() {
        sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("username",login_user_name.getText().toString());
        editor.putString("password",login_user_password.getText().toString());
        editor.commit();
//        Toast.makeText(getApplication(),"保存信息成功",Toast.LENGTH_SHORT).show();
    }
    private void getLoginInfo(){
        sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
        String username=sharedPreferences.getString("username","");
        login_user_name.setText(username);
        String password=sharedPreferences.getString("password","");
        login_user_password.setText(password);
    }
    private void submit() {
        // validate
        String name = login_user_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String password = login_user_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "password不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }
}
